package net.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.bo.KAffectation;
import net.bo.KPlan;
import net.bo.KUtilisateur;
import net.ko.converters.DateFormatter;
import net.ko.framework.KoSession;
import net.ko.http.objects.KRequest;
import net.ko.http.servlets.KSAction;
import net.ko.internet.KEmail;
import net.ko.internet.KServer;
import net.ko.kobject.KListObject;


/**
 * Servlet implementation class Form
 */
@WebServlet("/SSendMailPLan")
public class SSendMailPLan extends KSAction {
	private static final long serialVersionUID = 1L;
	
	      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SSendMailPLan() {
        super();
        // TODO Auto-generated constructor stub
    }   

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("deprecation")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		init(request, response);
		String valeurs;
		String dateD=KRequest.GETPOST("dateD", request);
		String dateF=KRequest.GETPOST("dateF", request);
		//récuperation paramêtre
		valeurs=KRequest.GETPOST("ids", request);	
		String[] tValeurs = valeurs.split(",");
		String idPlan=request.getSession().getAttribute("plan")+"";	
		KListObject<KPlan> plan=KoSession.kloadMany(KPlan.class, idPlan);
		String libPlan=plan.getFieldValues("libelle", "");
		KListObject<KUtilisateur> users= KoSession.kloadMany(KUtilisateur.class, tValeurs);
		String id=users.getFieldValues("id", ",");
		String[] tId=id.split(",");
		String login=users.getFieldValues("login", ",");
		String[] tLogin= login.split(",");
		String vals=users.getFieldValues("mail", ",");
		String[] tMail = vals.split(",");
		KEmail email=new KEmail();
		KServer server=new KServer("smtp.orange.fr","test","0000");
		server.setAuthentification(true);
		email.setMailServer(server);
		email.createMessage();
		for (int i=0;i<tMail.length;i++){
			for(int e=0;e<tLogin.length;e++){
			email.setTo(tMail[i]);
			email.setFrom("plan2tests@support.fr");
			email.setSubject("Affectation à un plan");
			email.setText(""
					+ "<div style='background: #d5cea6;"
					+ "background: -moz-linear-gradient(top,  #d5cea6 0%, #c9c190 40%, #b7ad70 100%);"
					+ "background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#d5cea6), color-stop(40%,#c9c190), color-stop(100%,#b7ad70));"
					+ "background: -webkit-linear-gradient(top,  #d5cea6 0%,#c9c190 40%,#b7ad70 100%);"
					+ "background: -o-linear-gradient(top,  #d5cea6 0%,#c9c190 40%,#b7ad70 100%);"
					+ "background: -ms-linear-gradient(top,  #d5cea6 0%,#c9c190 40%,#b7ad70 100%);"
					+"background: linear-gradient(top,  #d5cea6 0%,#c9c190 40%,#b7ad70 100%);"
					+"color:#fff;"
					+"padding:20px;"
					+"-webkit-border-radius: 10px 10px 10px 10px;"
					+"-moz-border-radius: 10px 10px 10px 10px;"
					+"border-radius:  10px 10px 10px 10px;"
					+"font-size:15px;'>"
					+""
					+"<img src='http://tests.kobject.net/images/main/logo-kobject-tests-fonctionnels.png'>"
					+ "<br><br>"
					+"<br> Bonjour"+" " +tLogin[i]+" " 
					+ "Vous avez été affecter au plan de test: "+libPlan
					+"<br>"
					+"<br>"
					+"Vous êtes affecté à ce plan de test du "+dateD+" au "+dateF+" ."
					+ "<br>"
					+ "Vous pouvez acceder à ce plan de test en vous connectant a votre compte:"
					+"<br>"
					+"<a href='localhost:8080/plan2tests/' target='localhost:8080/plan2tests/'>localhost:8080/plan2tests/</a>"
					+"<br><br>"
					+"<br>"
					+"<br><br>"
					+"<span style='font-size:11px'>Cette email a été envoyé automatiquement merci de ne pas répondre</span>"
					+"</div>");
			email.send(true);
			KAffectation aff= new KAffectation();
			aff.setIdUtilisateur(Integer.valueOf(tId[i]));
			aff.setIdPlan(Integer.valueOf(request.getSession().getAttribute("plan")+""));
			aff.setDateD(DateFormatter.getKoDate(dateD));	
			aff.setDateF(DateFormatter.getKoDate(dateD));
			KoSession.add(aff);
			}
		}
		
		out.print("Message Envoyé à: "+vals);
		
		
		

	}
}
